﻿#include "ssThreadLock.h"


namespace StarSeeker
{

ssThreadLock::ssThreadLock(void)
{
#ifdef SS_USE_BOOST_THREAD
	
#elif defined _WIN32
	InitializeCriticalSection(&m_mutex);
#else
    pthread_mutex_init(&m_mutex, 0);
#endif
    m_reference = 0;
}

ssThreadLock::~ssThreadLock(void)
{
	if (m_reference>0)
	{
		Unlock();
	}
#ifdef SS_USE_BOOST_THREAD
	
#elif defined _WIN32
	DeleteCriticalSection(&m_mutex);
#else
    pthread_mutex_destroy(&m_mutex);
#endif
}

void ssThreadLock::Lock()
{
#ifdef SS_USE_BOOST_THREAD
	m_mutex.lock();
#elif defined _WIN32
	EnterCriticalSection(&m_mutex);
#else
    pthread_mutex_lock(&m_mutex);
#endif
	++m_reference;
}

void ssThreadLock::Unlock()
{
	--m_reference;
#ifdef SS_USE_BOOST_THREAD
	m_mutex.unlock();
#elif defined _WIN32
	LeaveCriticalSection(&m_mutex);
#else
    pthread_mutex_unlock(&m_mutex);
#endif
}

bool ssThreadLock::IsLocked()
{
	return m_reference>0;
}

long ssThreadLock::GetRef()
{
	return m_reference.Value();
}

}
